package com.alice.common.utils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author alice
 * @date 2021/9/29
 * @apiNote
 */
public class FileUtils {

    public void testDownload(HttpServletResponse res) {
        String fileName = "aaa.txt";
        res.setHeader("content-type", "text/csv");
        //res.setHeader("content-type", "application/x-msdownload;");
        res.setContentType("text/csv; charset=utf-8");
        res.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os = null;

        try {
            os = res.getOutputStream();
            bis = new BufferedInputStream(new FileInputStream(new File("C://Users//lxn//Desktop//aaa.txt")));
            int i = bis.read(buff);

            while (i != -1) {
                os.write(buff, 0, buff.length);
                os.flush();
                i = bis.read(buff);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
